package com.cpi.cornpp.purchaseorder.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.cpi.cornpp.biweeks.view.BiWeekPeriodVersionView;
import com.cpi.cornpp.biweeks.view.BiWeekView;
import com.cpi.cornpp.blockdistribution.view.BlockDistributionView;
import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.plants.view.PlantView;
import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.purchaseorder.dto.PurchaseOrderVersionDTO;
import com.cpi.cornpp.purchaseorder.view.PurchaseOrderVersionView;
import com.cpi.cornpp.setregiondistribution.view.SetRegionDistributionView;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;
import com.cs.common.utils.NumberUtil;


public class PurchaseOrderVersionConverter {
	private static final Object PADDING_PARAMETER = "PADDING_FOR_CODE";	
	public static PurchaseOrderVersionView convert(PurchaseOrderVersionDTO dto){
		PurchaseOrderVersionView view = null;
		String postFix = null;
		BiWeekPeriodVersionView biWeekPeriodVersionView = null;
		BiWeekView biWeekView = null;
		SystemParameterBean param = (SystemParameterBean)SystemParameterCollection.getInstance().get(PADDING_PARAMETER);
		PlantView plantView = null;
		BlockDistributionView blockDistributionView = null;
		SetRegionDistributionView setRegionDistributionView = null;
		if (dto != null){
			view = new PurchaseOrderVersionView();
			
			view.setPurchaseOrderVersionId(dto.getPurchaseOrderVersionId());
			view.setPurchaseOrderId(dto.getPurchaseOrderId());
			view.setTransportId(dto.getTransportId());
			view.setBiWeekPeriodVersionId(dto.getBiWeekPeriodVersionId());
			view.setPlantId(dto.getPlantId());
			view.setCode(dto.getCode()!=null?dto.getCode().trim():dto.getCode());
			postFix = String.valueOf(dto.getPostFix()).trim();			
			view.setPostFix(org.apache.commons.lang.StringUtils.leftPad(postFix, param.getIntValue() ,"0"));
			view.setCapacity(dto.getCapacity());
			view.setApplyBalance(dto.getApplyBalance().intValue());
			view.setTransit(dto.getIsTransit()==1);
			view.setCornTypeId(dto.getCornTypeId().toString());
			if( dto.getBiWeekPeriodVersionYear() != null ){
				biWeekPeriodVersionView = new BiWeekPeriodVersionView();
				biWeekPeriodVersionView.setBiWeekPeriodVersionId(dto.getBiWeekPeriodVersionId());
				biWeekView = new BiWeekView();
				biWeekView.setDescription(dto.getBiWeekPeriodVersionDescription() + " " + dto.getBiWeekPeriodVersionYear().intValue());
				biWeekPeriodVersionView.setBiWeekView(biWeekView);
				view.setBiWeekPeriodVersionView(biWeekPeriodVersionView);
			}
			if(dto.getPlantShortName() != null){
				plantView = new PlantView();
				plantView.setPlantId(dto.getPlantId().intValue());
				plantView.setShortName(dto.getPlantShortName());
				view.setPlantView(plantView);
			}
			if(dto.getBlockStartDate()!=null){
				blockDistributionView = new BlockDistributionView();
				blockDistributionView.setPurchaseOrderVersionId(dto.getPurchaseOrderVersionId().intValue());
				blockDistributionView.setStartBlockPeriodDescription(dto.getBlockStartDate());
				blockDistributionView.setFinishBlockPeriodDescription(dto.getBlockFinishDate());
				blockDistributionView.setQuantity(NumberUtil.convertTon(dto.getBlockQuantity()));
				view.setBlockDistributionView(blockDistributionView);
			}
			if(dto.getRegionShortName()!=null){
				setRegionDistributionView = new SetRegionDistributionView();
				setRegionDistributionView.setRegionCode(dto.getRegionShortName());
				view.setSetRegionDistributionView(setRegionDistributionView);
			}
		}
		
		return view;
	}
	
	public static PurchaseOrderVersionDTO convert(PurchaseOrderVersionView view){
		PurchaseOrderVersionDTO dto = null;
		
		if (view != null){
			dto = new PurchaseOrderVersionDTO();
			
			dto.setPurchaseOrderVersionId(view.getPurchaseOrderVersionId());
			dto.setPurchaseOrderId(view.getPurchaseOrderId());
			dto.setTransportId(view.getTransportId());
			dto.setBiWeekPeriodVersionId(view.getBiWeekPeriodVersionId());
			dto.setPlantId(view.getPlantId());
			dto.setCode(view.getCode());
			dto.setPostFix(new Integer(view.getPostFix()));
			dto.setCapacity(view.getCapacity());
			dto.setCornTypeId(new Integer(view.getCornTypeId()));
		}
		
		return dto;
	}
	
	public static List convertDTOList(List dtoList){
		List viewList = null;
		viewList = new ArrayList(0);
		PurchaseOrderVersionDTO purchaseOrderVersionDTO = null;
		double capacityTotal = 0;
		PurchaseOrderVersionView purchaseOrderVersionView = null;
		if (CollectionUtils.isNotEmpty(dtoList)){
			for (Iterator itr = dtoList.iterator(); itr.hasNext(); ){
				purchaseOrderVersionDTO = (PurchaseOrderVersionDTO)itr.next();
				capacityTotal += purchaseOrderVersionDTO.getCapacity().doubleValue();
				viewList.add(convert(purchaseOrderVersionDTO));
			}
			if( viewList.size() > 0 ){
				purchaseOrderVersionView = new PurchaseOrderVersionView();
				purchaseOrderVersionView.setPurchaseOrderVersionId(new Integer(0));
				purchaseOrderVersionView.setCapacity(new Double(capacityTotal));
				viewList.add(purchaseOrderVersionView);
			}
		}
		
		return viewList;
	}
	
	public static List convertViewList(List viewList){
		List dtoList = null;
		
		dtoList = new ArrayList(0);
		
		if (CollectionUtils.isNotEmpty(viewList)){
			
			for (Iterator itr = viewList.iterator(); itr.hasNext(); ){
				dtoList.add(convert((PurchaseOrderVersionView) itr.next()));
			}
		}
		
		return dtoList;
	}
	/**
	 * Ordernes de Compa de Version DTOs a Vistas Converter
	 * Se utiliza para la lista de Cargos Manuales
	 * @author Edgar Joao
	 * @param dto
	 * @return
	 * @throws PurchaseProgramException
	 */
	public PurchaseOrderVersionView converterManualChargesDtoToView(PurchaseOrderVersionDTO dto) throws PurchaseProgramException {
		PurchaseOrderVersionView view = null;
		StringBuffer concatenate = null;
		try{
			if(dto != null){
				view = new PurchaseOrderVersionView();
				concatenate = new StringBuffer(0);
				view.setPurchaseOrderVersionId(dto.getPurchaseOrderVersionId());
				view.setPeriodDescription(dto.getPeriodDescription());
				concatenate.append(dto.getCode());
				concatenate.append(String.valueOf(NumberUtil.fill(dto.getCodeNumber().intValue(), '0', 3)));
				view.setCode(concatenate.toString());
				view.setCapacityStringValue(NumberUtil.convertTon(dto.getCapacity()));
				view.setAmount(NumberUtil.convertQuantity(dto.getAmount()));
			}
		}catch( Exception exception ){
			PurchaseProgramException purchaseProgramException=null;
			exception.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw purchaseProgramException;
		}
		return view;
	}
	
	private PurchaseOrderVersionView converterPurchaseOrderSortDtoToView(PurchaseOrderVersionDTO dto) throws PurchaseProgramException {
		PurchaseOrderVersionView view = null;
		StringBuffer concatenate = null;
		String postFix = null;
		try{
			SystemParameterBean param = (SystemParameterBean)SystemParameterCollection.getInstance().get(PADDING_PARAMETER);
			if(dto != null){
				view = new PurchaseOrderVersionView();
				
				view.setPurchaseOrderVersionId(dto.getPurchaseOrderVersionId());
				view.setPeriodDescription(dto.getPeriodDescription());
				
				postFix = String.valueOf(dto.getCodeNumber()).trim();
				concatenate = new StringBuffer(0);
				concatenate.append(dto.getCode().trim());
				concatenate.append(org.apache.commons.lang.StringUtils.leftPad(postFix, param.getIntValue() ,"0"));
				view.setCode(concatenate.toString());
				
				view.setCapacityStringValue(NumberUtil.convertTon(dto.getCapacity()));
				view.setFutureSort(String.valueOf(dto.getFutureSort()));
				view.setOriginalFutureSort(String.valueOf(dto.getFutureSort()));
			}
		}catch( Exception exception ){
			PurchaseProgramException purchaseProgramException=null;
			exception.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw purchaseProgramException;
		}
		return view;
	}
	/**
	 * Se utiliza para la lista de Cargos Manuales
	 * 
	 * @author Edgar Joao
	 * @param dtoList
	 * @return
	 * @throws PurchaseProgramException
	 */
	public List convertPurchasesOrdersViewList(List dtoList) throws PurchaseProgramException{
		return convertPurchasesOrdersViewList(dtoList, true);
	}
	
	public List convertPurchasesOrdersViewList(List dtoList, boolean charges) throws PurchaseProgramException{
		List viewList = null;
		Iterator iterator = null;
		PurchaseOrderVersionDTO dto = null;
		PurchaseOrderVersionView view = null;
		try{
			viewList = new ArrayList(0);			
			iterator = dtoList.iterator();
			while(iterator.hasNext()){
				dto = (PurchaseOrderVersionDTO) iterator.next();
				if( charges ){
					view = this.converterManualChargesDtoToView(dto);
				}else{
					view = this.converterPurchaseOrderSortDtoToView(dto);
				}
				viewList.add(view);
			}			
		}catch( Exception exception ){
			PurchaseProgramException purchaseProgramException=null;
			exception.printStackTrace(System.out);
			purchaseProgramException= new PurchaseProgramException(exception , CornPPException.LAYER_CONVERTER,CornPPException.ACTION_LISTS);
			throw purchaseProgramException;
		}
		return viewList;
	}
}